@extends('layouts.master')
@section('content')
    <div  class="content-wrapper" style="min-height: 850px;">
        <section class="content-header">
            <h3><i class="fa fa-star-half-full" aria-hidden="true"></i>设置配件</h3>
            <ol class="breadcrumb">
                <li>
                    <a href="{{ url('/') }}"><i class="fa fa-home"></i> Home</a>
                </li>
                <li>
                    <a href="">设置配件</a>
                </li>
            </ol>
        </section>

        <section class="content">
            <div class="col-xs-9">
                <a href="#"  class="btn btn-link btn-link-thin" id="addp">添加</a>

                <div class="box box-warning">
                    <div class="box-header with-border">
                        <h3 class="box-title"></h3>
                    </div>
                    <form  role="form" action="{{ url('product/setparts') }}" method="POST">
                        <input type="hidden" value={{$product_id}} name='product_id'/>

                        <!-- /.box-header -->
                        <div class="box-body">
                        {{ csrf_field() }}
                        <!-- text input -->
                            <table>
                                @forelse($parts as $p)
                                    <tr>
                                        <td>
                                            <div class="form-group  col-xs-12" >

                                                <select name="part_id[]" class="form-control">
                                                    <option value="0">--请选择配件--</option>
                                                    @foreach($list as $k => $part)
                                                        <option value='{{$k}}' {{ ($p->parts_id == $k)?'selected':''}}>{{$part}}</option>
                                                    @endforeach
                                                </select>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="form-group col-xs-12">
                                                <input type="text" class="form-control" placeholder="配件数量" name="num[]" value="{{$p->parts_num}}">
                                            </div>
                                        </td>
                                        <td>
                                            <div class="form-group col-xs-12">
                                                <a href="#"  class="btn btn-link btn-link-thin delp"  data-id="{{ $p->id }}" >删除</a>
                                            </div>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td>
                                            <div class="form-group  col-xs-12" >
                                                <select name="part_id[]" class="form-control">
                                                    <option>--请选择配件--</option>
                                                    @foreach($list as $k => $part)
                                                        <option value="{{ $k }}">{{ $part }}</option>
                                                    @endforeach
                                                </select>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="form-group col-xs-12">
                                                <input type="text" class="form-control" placeholder="配件数量" name="num[]">
                                            </div>
                                        </td>
                                        <td>
                                            <div class="form-group col-xs-12">
                                                <a href="#"  class="btn btn-link btn-link-thin delp">删除</a>
                                            </div>
                                        </td>
                                    </tr>
                                @endforelse
                            </table>
                        </div>
                        <div class="box-footer">
                            <button type="submit" class="btn btn-primary">提交</button>
                            <button type="reset" class="btn btn-default pull-right">重置</button>
                        </div>
                    </form>
                    <!-- /.box-body -->
                </div>
            </div>
        </section>
    </div>
@endsection
@section('js')
    <script>
        @if (count($errors) > 0)
            swal('', "{{ $errors->first() }}", 'warning');
        @endif

        $('#addp').on('click',function() {
            var ctr=$('tr:first').clone();
            ctr.find(":input").each(function(i){
                $(this).val("0");
            });
            $('.box-body').append(ctr)
        });



        $(document).on('click','.delp',function() {
            var tr_l=$(".box-body tr").length;
            if(tr_l==1){
                swal("至少一个配件")
            }else{
                $(this).parent().parent().parent().remove();
            }
        });

        $(".delp").on("click", function() {
            var _id = $(this).data("id");
            var token = "{{ csrf_token()  }}";
                    $.ajax({
                        url: "{{ url('product/deleteparts') }}",
                        data: {
                            _id: _id,
                            _token: token,
                        },
                        type: "post",
                        dataType: "json",
                    })
                });

    </script>
@endsection
